package org.lanqiao.servlet;

import org.lanqiao.entity.User;
import org.lanqiao.service.UserService;
import org.lanqiao.service.impl.UserServiceImpl;
import org.lanqiao.util.Pager;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
    private UserService service = new UserServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String way = request.getParameter("way");
        try {
            Method method = this.getClass().getDeclaredMethod(way, HttpServletRequest.class, HttpServletResponse.class);
            method.setAccessible(true);
            try {
                method.invoke(this, request, response);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取当前页参数【判断是否为空，设置默认值】
        String currPageNo = request.getParameter("currPageNo");
        int PageNo = 1;
        //当前页
        if (currPageNo != null) {
            PageNo = Integer.parseInt(currPageNo);
        }
        //2.调用后台的方法获取数据
        List<User> list = service.findAll();
        //3.创建分页类对象
        Pager<User> pager = new Pager<>();
        //4.给分页类对象设置总记录数，设置当前页，设置每页记录数
        //先设置每页记录数
        pager.setPageSize(3);
        //再设置总记录数
        pager.setList(list);
        //最后设置当前页
        pager.setCurrPageNo(PageNo);
        //5.获取当前页信息list
        List<User> sublist = pager.getList();
        //6.页面跳转，小背包里存数据
        request.setAttribute("list", sublist);
        request.setAttribute("pager", pager);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    protected void addUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String birthday = request.getParameter("birthday");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = new User(1, username, password, name, address, date);
        boolean flag = service.addUser(user);
        if (flag) {
            //成功
            response.sendRedirect("/UserServlet?way=findAll");
        } else {
            //失败
            request.setAttribute("msg", "添加失败");
            request.getRequestDispatcher("add.jsp").forward(request, response);

        }
    }

    protected void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        User user = service.findById(Integer.parseInt(id));
        request.setAttribute("user", user);
        request.getRequestDispatcher("show.jsp").forward(request, response);
    }

    protected void delUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        service.delUser(Integer.parseInt(id));
        response.sendRedirect("/UserServlet?way=findAll");
    }

    protected void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        User user = service.findById(Integer.parseInt(id));
        request.setAttribute("user", user);
        request.getRequestDispatcher("upd.jsp").forward(request, response);
    }

    protected void updUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String birthday = request.getParameter("birthday");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(birthday);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        User user = new User(1,username,password,name,address,date);
        service.updUser(user);
        response.sendRedirect("/UserServlet?way=findAll");
    }
}
